<?PHP
// vim: set expandtab tabstop=4 shiftwidth=4:
// +----------------------------------------------------------------------+
// | SAPID: XML Sapiens Engine Demonstrator                               |
// +----------------------------------------------------------------------+
// | Author:  Max Baryshnikov aka Mephius <mb@rg.by>                      |
// | Copyright (c) 2004 Max Baryshnikov                                   |
// | http://sapid.sourceforge.net                                             |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+
// Release: 13.11.04 (dd/mm/yy)
// $Id: image_resize.inc.php,v 1.4 2006/03/11 17:54:13 sapidaddons Exp $

function _sysB_chkgd2()
 {
        $rep=false;
        if(isset($GLOBALS["gBGDVersion"])) {
                $rep=$GLOBALS["gBGDVersion"];
        } else {
                //Edited by Zik
                $arr=get_loaded_extensions();
                if(in_array("gd", $arr) and function_exists(imagecreatetruecolor))
                        $GLOBALS["gBGDVersion"]=$rep="2.0";
                elseif (in_array("gd", $arr) and !function_exists(imagecreatetruecolor))
                        $GLOBALS["gBGDVersion"]=$rep="1.6";
        }
        return $GLOBALS["gBGDVersion"];
 }

function create_tnail($file, $w, $h, $path_prefix, $name_prefix, $unlink=false){
        $GLOBALS["gBGDVersion"]=_sysB_chkgd2();
        if(intval($GLOBALS["gBGDVersion"])>=2){
                $resample_function="imagecopyresampled";
                $create_function="imageCreateTrueColor";
        }else{
                $resample_function="imagecopyresized";
                $create_function="imageCreate";
        }


        $info=getimagesize($file["tmp_name"]);
        switch ($info[2]) {
                case 1:
                $img=imagecreatefromgif($file["tmp_name"]);
                break;
                case 2:
                $img=imagecreatefromjpeg($file["tmp_name"]);
                break;
                case 3:
                $img=imagecreatefrompng($file["tmp_name"]);
                break;
        }

        if(($info[0]/$info[1])<=($w/$h)){
                $new_w=$w;
                $new_h=(int)(($w/$info[0])*$info[1]);

                if( !@($tmp_im=$create_function($new_w, $new_h)) ) return false;
                $resample_function($tmp_im, $img, 0, 0, 0, 0, $new_w, $new_h, $info[0], $info[1]);
                imagedestroy($img);
                $im=($create_function($w, $h));
                $offset=(int)(($new_h-$h)/2);
                imagecopy($im, $tmp_im, 0,0,0,$offset,$w, $h);

                $filename=$path_prefix . $name_prefix . $file["name"];
                imagejpeg($im, $filename, 75);


                imagedestroy($im);
                imagedestroy($tmp_im);

                if ($unlink) @unlink($file["tmp_name"]);
        }elseif (($info[0]/$info[1])>($w/$h)){
                $new_h=$h;
                $new_w=(int)(($h/$info[1])*$info[0]);
                if( !@($tmp_im=$create_function($new_w, $new_h)) ) return false;
                $resample_function($tmp_im, $img, 0, 0, 0, 0, $new_w, $new_h, $info[0], $info[1]);

                imagedestroy($img);
                $im=$create_function($w, $h);
                $offset=(int)(($new_w-$w)/2);
                imagecopy($im, $tmp_im, 0,0,$offset,0,$w, $h);

                $filename=$path_prefix . $name_prefix . $file["name"];
                imagejpeg($im, $filename, 75);

                imagedestroy($im);
                imagedestroy($tmp_im);

                if ($unlink) @unlink($file["tmp_name"]);
        }
        return array("init_width"=>$info[0], "init_height"=>$info[1]);
}


//create_tnail(array("tmp_name"=>$_SERVER["DOCUMENT_ROOT"] . "/sapid/usr/content/images/1.jpg", "name"=>"1.jpg"), 200, 200, $_SERVER["DOCUMENT_ROOT"] . "/sapid/usr/content/images/", "icon_", false);
?>
